package com.lab.spring.topic2.messaging;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;

import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created with IntelliJ IDEA.
 * User: bsceran
 * Date: 5/22/13
 * Time: 12:48 PM
 * To change this template use File | Settings | File Templates.
 */
public class Cashier implements ApplicationEventPublisherAware {
    ApplicationEventPublisher applicationEventPublisher;
    private Date date;

    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    public void checkout(final double amount) {
        ExecutorService service =  Executors.newFixedThreadPool(10);
        service.execute(new Runnable() {
            @Override
            public void run() {
                CheckoutEvent checkoutEvent = new CheckoutEvent(this, amount, date);
                applicationEventPublisher.publishEvent(checkoutEvent);
            }
        });
        service.shutdown();
    }
}
